﻿Namespace Sistem.Kullanici
	Module Functions

		Sub Sil(Token As Token, kaykul As String)
			Token.ExecSQL("update users set status = 'P' where kaykul = @kaykul", kaykul)
		End Sub

		Function Source() As String
			Return "q@select kaykul,adi from users where isnull(status,'') = 'A' order by adi"
		End Function

		Sub SubeyeGonder(Token As Token, sube As Integer)
			Token.ExecSQL("sp_reportak 'users', @sube", sube)
		End Sub

		Function Oku(Token As Token, kaykul As String) As Recordset
			Return Token.OpenSQL("select * from users where kaykul = @kaykul", kaykul)
		End Function

		Function Yeni(Token As Token, username As String, adi As String, sube As String) As Result
			Dim Rs = Token.OpenSQL("select username from users where username = @username", username)
			If Not Rs.EOF Then Return Result.Error("Bu kullanıcı ismi kullanımdadır.")
			Rs = Token.OpenSQL("select max(kaykul) from users where kaykul like @sube + '%'", sube)
			Dim kaykul = Trim(Val(Rs(0)) + 1)
			Rs = Oku(Token, "")
			Rs.AddNew()
			Rs.SetValues("username,adi,bolum,password", username, adi, "", "123")
			Rs.SetValues("kaykul,status,hak,ssd2,email", kaykul, "A", "[S];[Q];[M];[Y];[E];[P];[K];[D];", "", "")
			Rs.Save()
			Return Result.OK(kaykul)
		End Function

		Sub Kaydet(Token As Token, kaykul As String, adi As String, ssd2 As String, hak As String)
			Dim Rs As Recordset = Oku(Token, kaykul)
			Rs.SetValues("adi,ssd2,hak", adi, ssd2, hak)
			Rs.Save()
		End Sub

		Sub YetkiAktar(Token As Token, kaykul1 As String, kaykul2 As String)
			Dim Rs1 = Token.OpenSQL("select * from users where kaykul = @kaykul", kaykul1)
			Dim Rs2 = Token.OpenSQL("select * from users where kaykul = @kaykul", kaykul2)
			Rs2.SetValues("hak,ssd2", Rs1("hak"), Rs1("ssd2"))
			Rs2.Save()
		End Sub

	End Module
End Namespace

Public Class Result

	Public ErrorMessage As String
	Public Kod As String
	Public Args As New SortedList(Of String, String)

	Shared Function [Error](Msg As String) As Result
		Dim e As New Result
		e.ErrorMessage = Msg
		Return e
	End Function

	Shared Function [OK](Kod As String) As Result
		Dim e As New Result
		e.Kod = Kod
		Return e
	End Function

	Function ErrorOccured() As Boolean
		Return ErrorMessage <> ""
	End Function

End Class